package cn.cxj.user.controller;

import cn.cxj.basic.exception.BusinessException;
import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.user.domain.User;
import cn.cxj.user.dto.UserDto;
import cn.cxj.user.query.UserQuery;
import cn.cxj.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口
 * @author chenxinjie
 * @date 2022-2-5 16:24:07
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 新增或者修改 POST
     * @param user 用户数据
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "用户添加或修改")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId() == null) {
                userService.add(user);
            } else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 删除 DELETE
     * @param id 用户id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "用户删除",notes = "删除时需要携带的id")
    @LogAnnotation(operateType = "用户删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "用户查询",notes = "查询时需要携带的id")
    @LogAnnotation(operateType = "用户查询")
    public User findById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    /**
     * 获取所有 GET
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部用户")
    @LogAnnotation(operateType = "查询全部用户")
    public List<User> findAll(){
        return userService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query 查询参数
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询和高级查询",notes = "查询时需要携带的参数")
    @LogAnnotation(operateType = "分页查询和高级查询")
    public PageList<User> pageQuery(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids 用户id数组
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "批量删除时需要携带的id集合")
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            userService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PostMapping("/phone/register")
    @LogAnnotation(operateType = "手机号注册")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PostMapping("/email/register")
    @LogAnnotation(operateType = "邮箱注册")
    public AjaxResult emailRegister(@RequestBody UserDto userDto){
        try {
            userService.emailRegister(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @GetMapping("/updateStateById")
    public AjaxResult updateStateById(Long id){

        try {
            userService.updateStateById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }
}
